package com.rcw.swiperefreshrecyclerview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by ruancw on 2018/3/29.
 * 自定义的加载更多view
 */

public class LoadMoreView extends LinearLayout {
    //加载更多的view
    private View loadMoreView;
    //加载更多及没有更多数据布局
    public RelativeLayout rlLoadMore,rlNoMoreData;
    //加载更多图片
    private ImageView ivLoadMore;
    //加载更多的提示信息
    private TextView tvMessage;
    //数值发生器
    private ValueAnimator animator;

    /**********************自定义加载更多view的构造函数*******************/

    public LoadMoreView(Context context) {
        this(context,null);
    }

    public LoadMoreView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadMoreView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 初始化布局文件
     */
    private void init() {
        loadMoreView =  LayoutInflater.from(getContext()).inflate(
                R.layout.load_more_view, null);
        rlLoadMore= (RelativeLayout) loadMoreView.findViewById(R.id.rl_load_more);
        rlNoMoreData= (RelativeLayout) loadMoreView.findViewById(R.id.rl_no_more_data);
        ivLoadMore = (ImageView) loadMoreView.findViewById(R.id.iv_load_more);
        tvMessage = (TextView) loadMoreView.findViewById(R.id.tv_message);
        //初始化数值发生器，下拉刷新时会调用，否则会报空指针
        animator = ValueAnimator.ofFloat(ivLoadMore.getRotation()
                ,ivLoadMore.getRotation()+359);
        addView(loadMoreView);
    }

    /**
     * 设置下拉刷新的布局背景
     * @param resId
     */
    public void setLoadMoreBackground(int resId) {
        loadMoreView.setBackgroundResource(resId);
    }

    /**
     * 设置加载更多时的图片
     * @param resId
     */
    public void setLoadMoreResource(int resId){
        ivLoadMore.setImageResource(resId);
    }

    /**
     * 设置加载更多图片的动画
     */
    public void startAnimation() {
        //animator需在构造函数中初始化，不然会报空指针
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setDuration(1000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ivLoadMore.setRotation((Float) animation.getAnimatedValue());
            }
        });
        animator.start();
        //设置加载更多图标的动画
       RotateAnimation animation = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setInterpolator(new LinearInterpolator());
        animation.setDuration(1000);
        animation.setRepeatCount(Animation.INFINITE);
        ivLoadMore.startAnimation(animation);
    }

    /**
     * 停止动画
     */
    public void stopAnimation() {
        animator.end();
    }

    /**
     * 加载完成
     * @param swipeRefreshRecyclerView 自定义的带上拉加载更多的recyclerView
     * @param isNoMoreData 是否有更多数据
     */
    public void loadMoreComplete(SwipeRefreshRecyclerView swipeRefreshRecyclerView, boolean isNoMoreData){
        if (isNoMoreData){
            rlLoadMore.setVisibility(GONE);//隐藏加载更多布局
            rlNoMoreData.setVisibility(VISIBLE);//显示没有更多数据布局
            stopAnimation();
            swipeRefreshRecyclerView.scrollBy(0,-swipeRefreshRecyclerView.lastVisibleItemPosition);
        }else {
            setVisibility(GONE);//隐藏掉所有的布局
            stopAnimation();
            //swipeRefreshRecyclerView.scrollBy(0,-getHeight());
            swipeRefreshRecyclerView.scrollBy(0,-swipeRefreshRecyclerView.lastVisibleItemPosition);

        }
        //加载完成时的提示信息
        Toast.makeText(getContext(), R.string.load_complete,Toast.LENGTH_SHORT).show();
    }

    /**
     * 加载失败时的操作
     * @param refreshRecyclerView
     */
    public void loadMoreFail(final SwipeRefreshRecyclerView refreshRecyclerView){
        tvMessage.setText(R.string.load_fail);//设置加载失败时的信息提示
        ivLoadMore.setVisibility(GONE);//隐藏加载更多时的图片
        //延迟500ms时间隐藏加载更多所有布局，并重新设置提示信息及加载更多时的转动图片
        postDelayed(new Runnable() {
            @Override
            public void run() {
                setVisibility(GONE);
                stopAnimation();
                refreshRecyclerView.scrollBy(0,-getHeight());
                tvMessage.setText(R.string.load_more);
                ivLoadMore.setVisibility(VISIBLE);
                //加载失败时的信息提示
                Toast.makeText(getContext(), R.string.load_fail,Toast.LENGTH_SHORT).show();
            }
        },500);
    }
}
